package com.ruoyi.shop.trade.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.shop.trade.mapper.ShopOrderDeliveryMapper;
import com.ruoyi.shop.trade.mapper.ShopOrderHistoryMapper;
import com.ruoyi.shop.trade.mapper.ShopOrderMapper;
import com.ruoyi.shop.common.constant.OrderConstants;
import com.ruoyi.shop.common.constant.PayConstants;
import com.ruoyi.shop.common.constant.ShopConstants;
import com.ruoyi.shop.common.exception.ShopException;
import com.ruoyi.shop.delivery.domain.ShopExpress;
import com.ruoyi.shop.delivery.mapper.ShopExpressMapper;
import com.ruoyi.shop.trade.domain.ShopOrder;
import com.ruoyi.shop.trade.domain.ShopOrderDelivery;
import com.ruoyi.shop.trade.domain.ShopOrderHistory;
import com.ruoyi.shop.trade.service.IShopOrderService;
import com.ruoyi.shop.trade.vo.ShopOrderStatusVo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 商城订单Service业务层处理
 * 
 * @author xuxiang
 * @date 2022-10-19
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService {
	@Autowired
	private ShopOrderMapper shopOrderMapper;
	@Autowired
	private ShopExpressMapper shopExpressMapper;
	@Autowired
	private ShopOrderDeliveryMapper shopOrderDeliveryMapper;
	@Autowired
    private ShopOrderHistoryMapper shopOrderHistoryMapper;
	
	/**
	 * 查询商城订单
	 * 
	 * @param orderId 商城订单主键
	 * @return 商城订单
	 */
	@Override
	public ShopOrder selectOrderById(Long orderId) {
		return shopOrderMapper.selectOrderById(orderId);
	}

	/**
	 * 查询商城订单列表
	 * 
	 * @param shopOrder 商城订单
	 * @return 商城订单
	 */
	@Override
	public List<ShopOrder> selectOrderList(ShopOrder shopOrder) {
		return shopOrderMapper.selectOrderList(shopOrder);
	}

	/**
	 * 新增商城订单
	 * 
	 * @param shopOrder 商城订单
	 * @return 结果
	 */
	@Override
	public int insertOrder(ShopOrder shopOrder) {
		return shopOrderMapper.insertOrder(shopOrder);
	}

	/**
	 * 修改商城订单
	 * 
	 * @param shopOrder 商城订单
	 * @return 结果
	 */
	@Override
	public int updateOrder(ShopOrder shopOrder) {
		return shopOrderMapper.updateOrder(shopOrder);
	}

	/**
	 * 批量删除商城订单
	 * 
	 * @param orderIds 需要删除的商城订单主键
	 * @return 结果
	 */
	@Override
	public int deleteOrderByIds(String orderIds) {
		return shopOrderMapper.deleteOrderByIds(Convert.toStrArray(orderIds));
	}

	/**
	 * 删除商城订单信息
	 * 
	 * @param orderId 商城订单主键
	 * @return 结果
	 */
	@Override
	public int deleteOrderById(Long orderId) {
		return shopOrderMapper.deleteOrderById(orderId);
	}

	/**
	 * 订单线下付款
	 * 
	 * @param orderId 商城订单主键
	 * @return 结果
	 */
	@Transactional
	@Override
	public int payOrder(Long orderId) {
		ShopOrder params = new ShopOrder();
		params.setOrderId(orderId);
		params.setPayType(PayConstants.PAY_TYPE_OFFLINE);// 付款类型-线下支付
		params.setPayStatus(ShopConstants.YES);// 付款状态-是
		params.setStatus(OrderConstants.ORDER_STATUS_DELIVERY);// 订单状态-待提货
		params.setPayTime(DateUtils.getNowDate());
		params.setUpdateTime(DateUtils.getNowDate());
		// 保存订单操作记录
		this.saveOrderHistroy(orderId, ShiroUtils.getUserId(), OrderConstants.ORDER_OPER_ACTION_PAY,
				OrderConstants.ORDER_OPER_HISTORY_PAY);
		return shopOrderMapper.updateOrder(params);
	}

	/**
	 * 订单配送(发货)
	 * 
	 * @param orderId
	 * @return
	 */
	@Transactional
	@Override
	public int deliveryOrder(ShopOrderDelivery request) {
		ShopOrder shopOrder = shopOrderMapper.selectOrderById(request.getOrderId());
		if (StringUtils.isNull(shopOrder)) {
			throw new ShopException("订单不存在");
		}

		ShopExpress shopExpress = shopExpressMapper.selectExpressById(request.getExpressId());
		if (StringUtils.isNull(shopExpress)) {
			throw new ShopException("物流公司不存在");
		}

		ShopOrderDelivery delivery = new ShopOrderDelivery();
		delivery.setOrderId(request.getOrderId());
		delivery.setContact(shopOrder.getContact());
		delivery.setMobile(shopOrder.getMobile());
		delivery.setAddress(shopOrder.getAddress());
		delivery.setProvinceId(shopOrder.getProvinceId());
		delivery.setProvinceName(shopOrder.getProvinceName());
		delivery.setCityId(shopOrder.getCityId());
		delivery.setCityName(shopOrder.getCityName());
		delivery.setDistrictId(shopOrder.getDistrictId());
		delivery.setDistrictName(shopOrder.getDistrictName());
		delivery.setExpressId(request.getExpressId());
		delivery.setExpressName(shopExpress.getExpressName());
		delivery.setExpressCode(shopExpress.getExpressCode());
		delivery.setDeliveryNo(request.getDeliveryNo());
		delivery.setDeliveryStatus(ShopConstants.YES);// 已发货
		delivery.setRogStatus(ShopConstants.NO);// 未收货
		delivery.setCreateTime(DateUtils.getNowDate());
		shopOrderDeliveryMapper.insertDelivery(delivery);
		ShopOrder order = new ShopOrder();
		order.setOrderId(request.getOrderId());
		order.setDeliveryId(delivery.getDeliveryId());
		order.setDeliveryNo(request.getDeliveryNo());
		order.setDeliveryTime(DateUtils.getNowDate());
		order.setDeliveryStatus(ShopConstants.YES);// 已配送
		order.setStatus(OrderConstants.ORDER_STATUS_ROG);
		// 保存订单操作记录
		this.saveOrderHistroy(request.getOrderId(), ShiroUtils.getUserId(), OrderConstants.ORDER_OPER_ACTION_DELIVERY,
				OrderConstants.ORDER_OPER_HISTORY_DELIVERY);
		return shopOrderMapper.updateOrder(order);
	}

	/**
	 * 订单状态数量统计
	 * 
	 * @return
	 */
	public ShopOrderStatusVo statusCount() {
		ShopOrderStatusVo vo = new ShopOrderStatusVo();
		int totalCount = this.queryStatusCount(null);// 总数量
		if (totalCount == 0) {
			vo.setPayCount(0);
			vo.setDeliveryCount(0);
			vo.setRogCount(0);
			vo.setFinishCount(0);
			vo.setCloseCount(0);
			return vo;
		}
		// 待付款-订单数量
		vo.setPayCount(this.queryStatusCount(OrderConstants.ORDER_STATUS_PAY));
		// 待发货-订单数量
		vo.setDeliveryCount(this.queryStatusCount(OrderConstants.ORDER_STATUS_DELIVERY));
		// 待收货-订单数量
		vo.setRogCount(this.queryStatusCount(OrderConstants.ORDER_STATUS_ROG));
		// 已完成-订单数量
		vo.setFinishCount(this.queryStatusCount(OrderConstants.ORDER_STATUS_FINISH));
		// 已关闭-订单数量
		vo.setCloseCount(this.queryStatusCount(OrderConstants.ORDER_STATUS_CLOASE));
		// 订单总数量
		vo.setTotalCount(totalCount);
		return vo;
	}
	
	
	/**
	 * 备注订单
	 * 
	 * @param shopOrder
	 * @return
	 */
	public int remarkOrder(ShopOrder shopOrder) {
		return shopOrderMapper.updateOrder(shopOrder);
	}

	/**
	 * 根据状态查询订单数量
	 * 
	 * @param status
	 * @return
	 */
	private Integer queryStatusCount(Integer status) {
		ShopOrder params = new ShopOrder();
		params.setStatus(status);
		return shopOrderMapper.queryStatusCount(params);
	}
	
	/**
	 * 保存订单操作记录
	 * 
	 * @param orderId
	 * @param userId
	 * @param operAction
	 * @param operHistory
	 */
	private void saveOrderHistroy(Long orderId, Long userId, String operAction, String operHistory) {
		ShopOrderHistory histroy = new ShopOrderHistory();
		histroy.setOrderId(orderId);
		histroy.setOperId(userId);
		histroy.setOperAction(operAction);
		histroy.setOperHistory(operHistory);
		histroy.setOperType(1);
		histroy.setOperTime(DateUtils.getNowDate());
		shopOrderHistoryMapper.insertHistory(histroy);
	}


}
